package com.bgs.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bgs.pojo.BeforeUser;
import com.bgs.service.BeforeUserService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DirectListener {
    @Autowired
    JavaMailSender sender;
    @Autowired
    private BeforeUserService beforeUserService;
    @RabbitListener(queues = "direct.test.queue")
    public void recive(Message message){
        String s = new String(message.getBody());
        HashMap hashMap = JSON.parseObject(s, HashMap.class);
        Object storeId = hashMap.get("storeId");
        int i = Integer.parseInt(storeId.toString());
        String email = beforeUserService.selectId(i);
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo(email);
        mailMessage.setText(hashMap.get("text").toString());
        sender.send(mailMessage);
    }
}
